package com.sk.sourcecircle.activity.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.home.activity.ChatActivity;
import com.sk.sourcecircle.adapter.MyFriendAdapter;
import com.sk.sourcecircle.base.BaseActivity;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.dialog.TipDialogFragment;
import com.sk.sourcecircle.entity.MyFriendBean;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.MyUtils;
import com.sk.sourcecircle.utils.SnackUtils;
import com.sk.sourcecircle.view.EasyLoadingMoreView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2018/1/18.
 */

public class AppFriendShareActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, HandleDataCallBack, EMCallBack {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;

    private int page = 1;
    private MyFriendAdapter adapter;
    private List<MyFriendBean.DataBeanX.DataBean> allDataList = new ArrayList<>();
    private boolean isLoadMore = false;
    private LoadingDialog loadingDialog;
    private MyFriendBean.DataBeanX.DataBean selectBean;

    private String targetUrl;
    private String describe;
    private String title;
    private String imageUrl;

    @Override
    protected int getContentView() {
        return R.layout.activity_app_share;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        title = getIntent().getStringExtra("title");
        targetUrl = getIntent().getStringExtra("targetUrl");
        imageUrl = getIntent().getStringExtra("imageUrl");
        describe = getIntent().getStringExtra("summary");

        loadingDialog = MyUtils.getLoadingDialog(mContext, false);

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new MyFriendAdapter(R.layout.item_pep, allDataList);
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        dataRyc.setAdapter(adapter);
        EasyLoadingMoreView loadMoreView = new EasyLoadingMoreView();
        adapter.setLoadMoreView(loadMoreView);
        View emptyView = getLayoutInflater().inflate(R.layout.empty_view, null);
        adapter.setEmptyView(emptyView);
        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, dataRyc);
        loadingDialog.show();
        AppApi.myFriend(page, 1, this, 0, this);
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (MyUtils.getUser(mContext).getHx_login() != null && MyUtils.getUser(mContext).getHx_login().length() > 0) {
            if (allDataList.get(position).getHx_login() != null && allDataList.get(position).getHx_login().length() > 0) {
                selectBean = allDataList.get(position);
                TipDialogFragment tipDialogFragment = new TipDialogFragment();
                tipDialogFragment.setMyCallBack(AppFriendShareActivity.this);
                tipDialogFragment.setContent("确认分享给该好友？");
                tipDialogFragment.show(getSupportFragmentManager(), "share");
            } else {
                SnackUtils.showError(mRootView, "该好友尚无聊天账号！");
            }
        } else {
            SnackUtils.showError(mRootView, "您尚未创建聊天账号！");
        }
    }

    @Override
    public void onLoadMoreRequested() {

    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        MyFriendBean myFriendBean = JSON.parseObject(result, MyFriendBean.class);
        List<MyFriendBean.DataBeanX.DataBean> dataBeanList = myFriendBean.getData().getData();
        allDataList.addAll(dataBeanList);
        if (allDataList.size() < 10) {
            adapter.setEnableLoadMore(false);
        } else {
            adapter.setEnableLoadMore(true);
        }
        if (isLoadMore) {
            adapter.loadMoreComplete();
            if (dataBeanList.size() == 0) {
                adapter.loadMoreEnd();
            }
            isLoadMore = false;
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void sure() {
        super.sure();
        EMMessage emMessage = EMMessage.createTxtSendMessage(title, selectBean.getHx_login());
        emMessage.setMessageStatusCallback(AppFriendShareActivity.this);
        emMessage.setAttribute("nickname", MyUtils.getName(mContext));
        emMessage.setAttribute("head_url", MyUtils.getHeadUrl(mContext));
        emMessage.setAttribute("user_id", MyUtils.getCode(mContext));
        emMessage.setAttribute("toName", selectBean.getUser_friend_info().getNickname());
        emMessage.setAttribute("toId", selectBean.getUser_friend_id());
        emMessage.setAttribute("toUrl", selectBean.getUser_friend_info().getHead_url());
        emMessage.setAttribute("targetUrl", targetUrl);
        emMessage.setAttribute("describe", describe);
        emMessage.setAttribute("imageUrl", imageUrl);
        EMClient.getInstance().chatManager().sendMessage(emMessage);
        EMClient.getInstance().chatManager().saveMessage(emMessage);
        loadingDialog.show();
    }

    @Override
    public void onSuccess() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                loadingDialog.cancelDialog();
                Toast.makeText(mContext, "分享成功！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(mContext, ChatActivity.class);
                intent.putExtra("id", selectBean.getHx_login());
                intent.putExtra("toName", selectBean.getUser_friend_info().getNickname());
                intent.putExtra("toUrl", selectBean.getUser_friend_info().getHead_url());
                intent.putExtra("toId", selectBean.getUser_friend_id());
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    public void onError(int code, String error) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                loadingDialog.cancelDialog();
                Toast.makeText(mContext, "分享失败！", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }

    @Override
    public void onProgress(int progress, String status) {

    }
}
